package net.starwifi.duobao.web.member;

import net.starwifi.duobao.entity.Area;
import net.starwifi.duobao.entity.Member;
import net.starwifi.duobao.mvc.Order;
import net.starwifi.duobao.entity.Receiver;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.AreaService;
import net.starwifi.duobao.service.MemberService;
import net.starwifi.duobao.service.ReceiverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/11/17.
 */
@Controller("apiReceiverController")
@RequestMapping("/member/receiver")
public class ReceiverController extends BaseController {

    @Autowired
    AreaService areaService;

    @Autowired
    ReceiverService receiverService;

    @Autowired
    MemberService memberService;

    @RequestMapping(value = "/pick", method = RequestMethod.GET)
    public String view(Model model,Long id,String tag) {
        Member member=memberService.getCurrent();
        model.addAttribute("tag",tag);
        model.addAttribute("receiverList", receiverService.getListData("o.member.id=?1",new Object[]{member.getId()}));
        return "/member/address/list";
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model,String tag) {
        List<Order> orderBy = new ArrayList<Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        Member member=memberService.getCurrent();
        model.addAttribute("tag",tag);
        model.addAttribute("receiverList", receiverService.getListData("o.member.id=?1",new Object[]{member.getId()},orderBy));
        return "/member/receiver/list";
    }


    /**
     * 添加收获地址
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addressAdd(Model model,String redirectUrl,String tag) {
        final List<Area> areas=  areaService.getCacheListData(" o.parent is null", new Object[]{});
        model.addAttribute("areas",areas);
        model.addAttribute("tag",tag);
        model.addAttribute("redirectUrl",redirectUrl);
        return "/member/receiver/add";
    }

    /**
     * 添加收获地址提交
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Message addressSave(Receiver entity,Long province,Long city,Long district){
        Area area=null;
        if(district==0){
            entity.setArea(areaService.find(city));
        }else {
            entity.setArea(areaService.find(district));
        }
        entity.setIsDefault(Boolean.TRUE);
        entity.setMember(memberService.getCurrent());
        receiverService.save(entity);
        return Message.success("地址添加成功");
    }

    /**
     * 添加收获地址
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String addressEdit(Model model,Long id,String redirectUrl,String tag) {
        Receiver receiver=receiverService.find(id);
        String[] areaPath=receiver.getArea().getTreePath().split(",");
        model.addAttribute("provinceAreaId",areaPath[1]);
        if(areaPath.length>2){
            model.addAttribute("cityAreaId", areaPath[2]);
            model.addAttribute("districtAreaId", receiver.getArea().getId());
        }else{
            model.addAttribute("cityAreaId", receiver.getArea().getId());
            model.addAttribute("districtAreaId",0);
        }
        model.addAttribute("entity", receiver);
        model.addAttribute("tag",tag);
        model.addAttribute("redirectUrl", redirectUrl);
        return "/member/receiver/edit";
    }


    /**
     * 收获地址提交
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Message addressUpdate(Receiver entity,Long province,Long city,Long district) {
        entity.setIsDefault(Boolean.TRUE);
        if(district==0){
            entity.setArea(areaService.find(city));
        }else {
            entity.setArea(areaService.find(district));
        }
        entity.setMember(memberService.getCurrent());
        receiverService.update(entity);
        return Message.success("地址修改成功");
    }


}
